package com.jye.rseven.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Task;
import com.jye.rseven.service.TaskService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/task")
@Api(tags = "任务控制器API")
public class TaskController {
    @Reference
    TaskService taskService;

    /*
    查看某一天的打卡记录
     */
    @GetMapping("/findDailyTaskByCreateTime")
    @ApiOperation(value = "查找某一天的日常打卡", notes = "根据任务的时间查找日常打卡", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "createTime",
                    value = "任务时间", required = true,
                    dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findDailyTaskByCreateTime(String createTime, Integer uId) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        Task t = taskService.findTaskByCreateTimeAndType(createTime, uId, type);
        if (!StringUtils.isEmpty(t)) {
            jsonResult.setData(t);
        } else {
            jsonResult.setMsg("未查到记录");
        }
        return jsonResult;
    }

    /*
     * 通过id查找任务
     * */
    @GetMapping("/findTaskById")
    @ApiOperation(value = "查找日常打卡", notes = "根据任务id查找日常打卡", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "任务ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findTaskById(int id) {
        JsonResult jsonResult = new JsonResult();
        Task t = taskService.findTaskById(id);
        if (!StringUtils.isEmpty(t)) {
            jsonResult.setData(t);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findMyActivitiesTaskByUId")
    @ApiOperation(value = "查找我的所有活动任务", notes = "根据用户id:uId查找活动任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyActivitiesTaskByUId(int uId, int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.activities;
        List<Task> list = taskService.findAllTaskByUIdAndType(uId, type, skip, limit);
        if (list.size() != 0) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
  * 查找任务
  * */
    @GetMapping("/findMyDailyTaskByUId")
    @ApiOperation(value = "查找我的所有日常打卡", notes = "根据用户id:uId查找所有日常打卡", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyDailyTaskByUId(int uId) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        List<Task> list = taskService.findAllTaskByUIdAndTypeNoLimit(uId,type);
        if (list.size() != 0) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findMyDailyTaskByUIdNoLimit")
    @ApiOperation(value = "查找我的所有日常打卡(日历)", notes = "根据用户id:uId查找所有日常打卡", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyDailyTaskByUIdNoLimit(int uId) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        List<Task> list = taskService.findAllTaskByUIdAndTypeNoLimit(uId, type);
        if (list.size() != 0) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    @GetMapping("/findActivitiesTaskByPId")
    @ApiOperation(value = "通过pId查找活动任务", notes = "通过pId查找活动任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pId",
                    value = "pId", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findActivitiesTaskByPId(int pId, int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.activities;
        List<Task> list = taskService.findTasksByPIdAndType(pId, type, skip, limit);
        if (list.size() != 0) {
            for (Task task : list) {
                //投票数量
                task.setVoteCount(taskService.findTaskVoteCountById(task.getId()));
            }
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findDailyByPId")
    @ApiOperation(value = "通过pId查找日常任务", notes = "通过pId查找日常任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pId",
                    value = "pId", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findDailyBypId(int pId, int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        List<Task> list = taskService.findAllTasksByPIdAndType(pId, type, skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    /*
     * 查找我的任务
     * */
    @GetMapping("/findDailyByUId")
    @ApiOperation(value = "查找我的日常任务", notes = "根据类型查找我的任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findDailyByUId(int uId, int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        List<Task> list = taskService.findAllTaskByUIdAndType(uId, type, skip, limit);
        if (list.size() != 0) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("未查询到记录，查询失败");
        }
        return jsonResult;
    }

    @PostMapping("/addActivitiesTask")
    @ApiOperation(value = "添加活动作品", notes = "根据描述desc,图片imgUrl,xxx添加活动作品", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "task",
                    value = "任务实体类", required = true),
    })
    public JsonResult addActivitiesTask(Task task) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.activities;
        //查看pid是否有参加过活动
        Task t = taskService.findTaskByUIdAndPId(task.getUId(), task.getPId(), type);
        task.setType(type);
        if (t != null) {
            jsonResult.setCode("500");
            jsonResult.setMsg("已经参赛过了!请勿重复参赛!");
        } else {
            int i = taskService.addTask(task);
            if (i == 1) {
                jsonResult.setData("参赛成功!");
            } else {
                jsonResult.setData("参赛失败!");
                jsonResult.setCode("500");
            }
        }

        return jsonResult;

    }

    /*
     * 增加任务
     * */
    @PostMapping("/addDailyTask")
    @ApiOperation(value = "添加日常任务", notes = "根据任务实体添加实体", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "task",
                    value = "任务实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Task"),
    })
    public JsonResult addDailyTask(Task task) {
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.taskType.daily;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Task t = taskService.findTaskByCreateTimeAndType(df.format(new Date()), task.getUId(), type);
        task.setType(type);
        if (t != null) {
            jsonResult.setMsg("已经打过卡了");
        } else {
            int i = taskService.addTask(task);
            if (i == 1) {
                jsonResult.setData("添加成功!");

            } else {
                jsonResult.setData("添加失败!");
                jsonResult.setCode("500");
            }
        }
        return jsonResult;
    }

    /*
     * 修改任务信息
     * */
    @PutMapping("/updateTask")
    @ApiOperation(value = "修改任务信息", notes = "修改任务信息,只能修改标题与描述", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "task",
                    value = "任务实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Task"),
    })
    public JsonResult updateTask(Task task) {
        JsonResult jsonResult = new JsonResult();
        int i = taskService.updateTask(task);
        if (i == 1) {
            jsonResult.setData("修改成功!");

        } else {
            jsonResult.setData("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @DeleteMapping("/deleteTaskById")
    @ApiOperation(value = "删除任务", notes = "删除任务信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "任务id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult deleteTaskById(int id) {
        JsonResult jsonResult = new JsonResult();
        int i = taskService.deleteTaskById(id);
        if (i == 1) {
            jsonResult.setData("修改成功!");

        } else {
            jsonResult.setData("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @PostMapping("/voteActivitiesTask")
    @ApiOperation(value = "投票活动作品", notes = "通过作品id和用户id投票活动作品", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "作品Id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult voteActivitiesTask(int id, int uId) {
        JsonResult jsonResult = new JsonResult();
        Boolean flag = taskService.isVote(id, uId);
        if (flag) {
            //投过了
            return new JsonResult("500", "已经为此次活动投过票了!", null);
        } else {
            //未投
            int i = taskService.voteActivitiesTask(id, uId);
            if (i != 0) {
                return new JsonResult("200", "投票成功!!", null);
            } else {
                return new JsonResult("500", "投票失败!!", null);
            }
        }
    }


    @GetMapping("/findDailyTask")
    @ApiOperation(value = "用户查找最近所有人的打卡", notes = "根据用户id:uId用户查找最近所有人的打卡", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", dataType = "Integer"),
    })
    public JsonResult findDailyTask(int uId,
                                    @RequestParam(required = false) Integer skip,
                                    @RequestParam(required = false) Integer limit) {
        JsonResult jsonResult = new JsonResult();
        if (skip == null && limit == null) {
            skip = 0;
            limit = 30;
        }
        int type = MyConstants.taskType.daily;
        List<Task> taskList = taskService.findAllTaskByUIdAndType(uId,type,skip,limit);
        if (taskList.size() != 0) {
            jsonResult.setData(taskList);
        } else {
            jsonResult.setMsg("暂无打卡数据!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


}
